/*
 * @Description: 
 * @Version: 1.0
 * @Autor: caohq33221
 * @Date: 2021-03-09 11:18:08
 * @LastEditors: codercao
 * @LastEditTime: 2021-07-19 11:26:48
 */
import moment from 'moment'
export const delReport = (data) => {
  data.map((item) => {
    item.caption = item.title
    if (!item.column) {
      item.fieldname = item.field
    }
    delete item.width
    delete item.minWidth
    delete item.field
    delete item.title
    delete item.slot
    delete item.className
    if (item.column) {
      item.columns = delReport(item.column)
      delete item.column
    }
  })
  return data
}

// 处理数组里保留小数
//data数组  noDelList不处理的keyList, fixed保留位小数处理
export const delDataToFixed = (data, noDelList, fixed) => {

  if (data.length < 1) return []
  let dataL = []
  let nodel = noDelList ? noDelList : []

  let fixedLength = fixed ? fixed : 4   // 默认数据保留4位小数处理
  data.map((item) => {
    let value = Object.values(item)
    let keys = Object.keys(item)
    let items = {}
    keys.map((itemkeys, itemindex) => {
      let val = value[itemindex]
      if (
        typeof value[itemindex] == 'number' &&
        !nodel.includes(itemkeys)
      ) {
        val = value[itemindex].toFixed(fixedLength) + ''
        let intSum = val
          .substring(0, val.indexOf('.'))
          .replace(/\B(?=(?:\d{3})+$)/g, ',')
        let dot = val.substring(val.length, val.indexOf('.'))
        val = intSum + dot
        items[itemkeys] = val
      } else {
        items[itemkeys] = (!val && val != 0) ? "--" : val
      }
      return itemkeys
    })
    dataL.push(items)
  })
  return dataL
}
// 获取随机数
export const randomString = (e) => {
  e = e || 32;
  var t = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ",
    a = t.length,
    n = "";
  for (let i = 0; i < e; i++) n += t.charAt(Math.floor(Math.random() * a));
  return n
}

// 获取交易日期（不含节假日）
export const getTradingDate = (date = new Date(), format = "YYYY-MM-DD") => {
  if (moment(date).format('dddd') === '星期六' || moment(date).format('dddd') === 'Saturday') {
    return moment(date).subtract(1, 'days').format(format)
  }
  if (moment(date).format('dddd') === '星期日' || moment(date).format('dddd') === 'Sunday') {
    return moment(date).subtract(2, 'days').format(format)
  }
  return moment(date).format(format)

}